package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/9 8:54
 */
public class 二叉搜索树中的搜索700 {

    TreeNode ans;
    int target;

    public TreeNode searchBST(TreeNode root, int val) {
        target = val;
        recur(root);
        return ans;
    }

    public void recur(TreeNode node) {
        if (node == null) {
            return;
        }
        int val = node.val;
        if (val == target) {
            ans = new TreeNode(val);
            ans.left = node.left;
            ans.right = node.right;
        }
        //如果说已经找到了, 就不往下找了,直接返回了
        if (ans != null) {
            return;
        }
        recur(node.left);
        recur(node.right);
    }

}
